﻿using SK3000RunningTestTool.SKServerDelegate;
using SK3000RunningTestTool.SKServerEnum;
using SK3000RunningTestTool.SKServerSource;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace SK3000RunningTestTool.SKService
{
    public class Service_TCPService : ISKService
    {
        public SKServer Server
        {
            get;
            set;
        }
        public int ID
        {
            get;
            set;
        }
        private Enum_ServiceTestType typeID = Enum_ServiceTestType.TCPService;
        /// <summary>
        /// 类型ID
        /// </summary>
        public Enum_ServiceTestType TypeID
        {
            get { return typeID; }
            set { }
        }
        public ServiceTestType Type => ServiceTestTypeSource.ServiceTestTypeList[typeID];
        string strServiceName = "客户端服务";
        public string ServiceName
        {
            get { return strServiceName; }
            set { strServiceName = value; }
        }

        public string ServiceRemark
        {
            get;
            set;
        }
        public string ServiceAddress
        {
            get;
            set;
        }

        public ushort ServicePort
        {
            get;
            set;
        }

        public object Tag
        {
            get;
            set;
        }


        public event ServiceTestProgressDelegate ServiceTestProgressEvent;

        private void ServiceTestProgress(ServiceTestProgressValue value)
        {
            if (ServiceTestProgressEvent != null)
            {
                ServiceTestProgressEvent(this, value);
            }
        }



        public event ServiceTestCallbackDelegate ServiceTestCallbackEvent;

        public void ServiceTestCallback(ServiceTestCallbackValue value)
        {
            if (ServiceTestCallbackEvent != null)
            {
                ServiceTestCallbackEvent(this, value);
            }
        }



        /// <summary>
        /// 服务器测试 
        /// </summary>
        /// <returns></returns>
        public ServiceTestCompleteValue ServiceTest()
        {
            ServiceTestCallback(new ServiceTestCallbackValue { EventValue = Enum_ServiceTestEvent.ServiceTestStart });
            ServiceTestCallback(new ServiceTestCallbackValue { EventValue = Enum_ServiceTestEvent.ConnectServiceing });
            ServiceTestProgress(new ServiceTestProgressValue { ProgressValue = 0 });
            ServiceTestCompleteValue result = new ServiceTestCompleteValue
            {
                reuslt = Enum_ServiceTestResult.Fail,
            };
            //转警服务器服务器测试
            //测试条目 1. 连接服务器端口
            TcpClient client = new TcpClient();
            try
            {
                ServiceTestProgress(new ServiceTestProgressValue { ProgressValue = 10 });
                client.Connect(ServiceAddress, ServicePort);
                ServiceTestCallback(new ServiceTestCallbackValue { EventValue = Enum_ServiceTestEvent.ConnectSuccess });
                result.reuslt = Enum_ServiceTestResult.Normal;
            }
            catch (Exception ex)
            {

                ServiceTestCallback(new ServiceTestCallbackValue { EventValue = Enum_ServiceTestEvent.ConnectException });
                result.reuslt = Enum_ServiceTestResult.TestException;
                result.Para1 = ex;
            }
            finally
            {

                ServiceTestProgress(new ServiceTestProgressValue { ProgressValue = 70 });
                client.Close();
                ServiceTestProgress(new ServiceTestProgressValue { ProgressValue = 90 });
            }
            ServiceTestCallback(new ServiceTestCallbackValue { EventValue = Enum_ServiceTestEvent.ConnectServiced });

            //测试条目 2. 发送登陆数据
            ServiceTestProgress(new ServiceTestProgressValue { ProgressValue = 100 });

            ServiceTestCallback(new ServiceTestCallbackValue { EventValue = Enum_ServiceTestEvent.ServiceTestEnd });
            return result;
        }
        public void ServiceTestAsync(ServiceTestCompleteDelegate callback)
        {
            Timer timTimeOutVideoClose = new Timer(new TimerCallback(timTimtOutVideoClose_Event), callback, 0, Timeout.Infinite);
            return;
        }

        private void timTimtOutVideoClose_Event(object sender)
        {
            ServiceTestCompleteValue value = ServiceTest();
            ServiceTestCompleteDelegate call = (ServiceTestCompleteDelegate)sender;
            if (call != null)
            {
                call(this, value);
            }
        }
    }
}
